package com.geekcoding.geekoj.controller.admin;

import com.geekcoding.common.dto.ApiResponse;
import com.geekcoding.geekoj.dto.request.QuestionEditReq;
import com.geekcoding.geekoj.entity.Question;
import com.geekcoding.geekoj.entity.TestAnswer;
import com.geekcoding.geekoj.mapper.QuestionMapper;
import com.geekcoding.geekoj.repository.QuestionRepository;
import com.geekcoding.geekoj.repository.TestAnswerRepository;
import com.geekcoding.geekoj.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("/api/admin/question")
public class AdminQuestionController extends BaseAdminController<Question, Question, QuestionMapper, QuestionRepository>{

    private final TestAnswerRepository testAnswerRepository;
    public AdminQuestionController(QuestionRepository repository, UserService userService, TestAnswerRepository testAnswerRepository) {
        super(repository, userService);
        this.testAnswerRepository = testAnswerRepository;
    }

    @Override
    public void saveOrUpdatePre(Question req) {
        super.saveOrUpdatePre(req);
        req.setCreatorId(userService.currentUser().getId());
    }

    @GetMapping("kanban")
    public ApiResponse kanbanData(){
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> countMaps = testAnswerRepository.rankingByCount();
        List<Map<String, Object>> yesMaps = testAnswerRepository.rankingByYes();
        List<Map<String, Object>> noMaps = testAnswerRepository.rankingByNo();
        data.put("totalRanks", countMaps);
        data.put("yesRanks", yesMaps);
        data.put("noRanks", noMaps);
        return ApiResponse.success(data);
    }
}
